package cmd

import (
	"github.com/rs/zerolog"
	"github.com/spf13/cobra"
	"pqstat/pkg/config"
	"pqstat/pkg/dao"
	"pqstat/pkg/log"
	"pqstat/pkg/queue"
	"pqstat/pkg/queue/adapter"
)

var queueType string

func init() {
	var sp = RunCommand.PersistentFlags()
	sp.StringVar(&queueType, "queue", "redis", "What kind of queue to use")
	sp.Uint8Var(&logLevel, "loglevel", 0, "Log level")
}

var RunCommand = &cobra.Command{
	Use:   "run",
	Short: "Statistic things",
	Run: func(cmd *cobra.Command, args []string) {
		var rq queue.Queue
		if queueType == "redis" {
			rq = &adapter.RedisQueue{}
		}
		zerolog.SetGlobalLevel(zerolog.Level(logLevel))
		dao.Connect()
		for _, qconfig := range config.QueueConfigs {
			log.Info("Consuming channel:" + qconfig.Channel)
			go queue.SetupAndConsume(rq, qconfig.Channel, qconfig.Cbs)
		}
	},
}
